﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QApplication>
#include <QFontMetrics>

// custom headers
#include "ParameterItemDelegate.h"
#include "ParametersTableSymbols.h"
#include "DebugMessage.h"


#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs 
{
    ParameterItemDelegate::ParameterItemDelegate(QObject *parent) : QStyledItemDelegate(parent) 
    {
    }
    
    ParameterItemDelegate::~ParameterItemDelegate()
    {
    }

    void ParameterItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
        int32_t col = index.column();
        DBG_WARN_AND_RETURN_VOID_UNLESS(index.model(), L"index.model()为空", L"GMEP", L"2024-01-30");
        DBG_WARN_AND_RETURN_VOID_UNLESS(QApplication::style(), L"QApplication::style()为空", L"GMEP", L"2024-01-30");
        if (col == COL_PARAMETER_FLAG)
        {
            // temporarily hide instance flag
            return;

            QString text = index.model()->data(index, Qt::DisplayRole).value<QString>();
            if (text.length() > 0)
            {
                painter->save();

                QFont font(QString::fromStdWString(L"Microsoft YaHei"));
                font.setPixelSize(12);
                
                // draw background
                QFontMetrics fontMetric(font);
                QRect textLocalRect = fontMetric.tightBoundingRect(text);
                QMargins margin(8, 0, 0, 0);
                QRect textRect(option.rect.x() + margin.left(), option.rect.y() + margin.top(), textLocalRect.width(), textLocalRect.height());
                QRectF borderRect = textRect.adjusted(-2, -2, 2, 2);
                QPainterPath borderPath;
                int32_t radius = 2;
                borderPath.moveTo(borderRect.bottomRight() - QPointF(0, radius));
                borderPath.lineTo(borderRect.topRight() + QPointF(0, radius));
                borderPath.arcTo(QRectF(borderRect.topRight() - QPointF(radius * 2, 0), QSize(radius * 2, radius * 2)), 0, 90);
                borderPath.lineTo(borderRect.topLeft() + QPointF(radius, 0));
                borderPath.arcTo(QRectF(borderRect.topLeft(), QSize(radius * 2, radius * 2)), 90, 90);
                borderPath.lineTo(borderRect.bottomLeft() - QPointF(0, radius));
                borderPath.arcTo(QRectF(borderRect.bottomLeft() - QPointF(0, radius * 2), QSize(radius * 2, radius * 2)), 180, 90);
                borderPath.lineTo(borderRect.bottomRight() - QPointF(radius, 0));
                borderPath.arcTo(QRectF(borderRect.bottomRight() - QPointF(radius * 2, radius * 2), QSize(radius * 2, radius * 2)), 270, 90);
                painter->fillPath(borderPath, QBrush(QColor(176, 178, 185, 255)));

                // draw text
                painter->setFont(font);
                QPalette palette = QApplication::palette();
                palette.setColor(QPalette::ColorRole::Foreground, Qt::white);
                QApplication::style()->drawItemText(painter, textRect, Qt::AlignCenter, palette, true, text, QPalette::ColorRole::Foreground);

                painter->restore();
            }
        }
        else if (col == COL_PARAMETER_NAME)
        {
            QString text = index.model()->data(index, Qt::DisplayRole).value<QString>();
            if (text.length() > 0)
            {
                painter->save();
                
                QFont font(QString::fromStdWString(L"Microsoft YaHei"));
                font.setPixelSize(12);
                painter->setFont(font);
                painter->setPen(QPen(QColor(120, 122, 131, 255), 1.0));
                QMargins margin(8, 0, 0, 0);
                QTextOption textOptions(Qt::AlignmentFlag::AlignVCenter | Qt::AlignmentFlag::AlignRight);
                textOptions.setWrapMode(QTextOption::WrapMode::WordWrap); // 换行
                painter->drawText(option.rect.marginsRemoved(margin), text, textOptions);

                painter->restore();
            }
        }
        else if (col == COL_PARAMETER_VALUE)
        {
            QString text = index.model()->data(index, Qt::DisplayRole).value<QString>();
            if (text.length() > 0)
            {
                painter->save();

                QFont font(QString::fromStdWString(L"Microsoft YaHei"));
                font.setPixelSize(12);
                painter->setFont(font);
                QPalette palette = QApplication::palette();
                palette.setColor(QPalette::ColorRole::Foreground, QColor(80, 82, 91, 255));
                QMargins margin(8, 0, 8, 0);
                QRect textRect = option.rect.adjusted(margin.left(), margin.top(), -margin.right(), -margin.bottom());
                auto elidedText = QFontMetrics(font).elidedText(text, Qt::TextElideMode::ElideRight, textRect.width());
                QApplication::style()->drawItemText(
                    painter,
                    textRect,
                    Qt::AlignmentFlag::AlignVCenter | Qt::AlignmentFlag::AlignLeft,
                    palette,
                    true,
                    elidedText,
                    QPalette::ColorRole::Foreground);

                painter->restore();
            }
        }
        else
        {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }

    bool ParameterItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
    {
        int32_t col = index.column();
        if (col == COL_PARAMETER_FLAG)
        {

        }
        else if (col == COL_PARAMETER_NAME)
        {

        }
        else if (col == COL_PARAMETER_VALUE)
        {

        }
        else
        {
            return QStyledItemDelegate::editorEvent(event, model, option, index);
        }
        return QStyledItemDelegate::editorEvent(event, model, option, index);
    }
}
